package com.sky.myapplication.java.utils;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.MediaStore;
import android.util.Log;

import com.sky.myapplication.java.data.VideoEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/24.
 */

public class FileUtil {

    public static List<VideoEntity> getAllText(Activity context) {
        ContentResolver mContentResolver = context.getContentResolver();

        List texts = new ArrayList<VideoEntity>();
        String[] projection = new String[]{MediaStore.Files.FileColumns._ID, MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.TITLE, MediaStore.Files.FileColumns.MIME_TYPE};

        //相当于我们常用sql where 后面的写法
        String selection = MediaStore.Files.FileColumns.MIME_TYPE + "= ? "
                + " or " + MediaStore.Files.FileColumns.MIME_TYPE + " = ? "
                + " or " + MediaStore.Files.FileColumns.MIME_TYPE + " = ? "
                + " or " + MediaStore.Files.FileColumns.MIME_TYPE + " = ? ";

        String[] selectionArgs = new String[]{"application/msword", "application/pdf", "application/vnd.ms-powerpoint", "application/vnd.ms-excel"};
//        String[] selectionArgs = new String[]{"text/plain", "application/msword", "application/pdf", "application/vnd.ms-powerpoint", "application/vnd.ms-excel"};
        Cursor cursor = mContentResolver.query(MediaStore.Files.getContentUri("external"), projection, selection, selectionArgs, MediaStore.Files.FileColumns.DATE_MODIFIED + " desc");
        String fileId;
        String fileName;
        String filePath;

        while (cursor.moveToNext()) {

            fileId = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns._ID));
            fileName = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.TITLE));
            filePath = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA));

            Log.e("ryze_text", fileId + " -- " + fileName + " -- " + "--" + cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.MIME_TYPE)) + filePath);
            VideoEntity info = new VideoEntity();
            info.filePath = filePath;
            info.title = fileName;
            texts.add(info);
        }
        cursor.close();
        cursor = null;

        return texts;

    }
}
